package src.算法.查询;

public class A01二分查找法02 {
    //    [1,3,5,7,9]
//    2
    /*
     *  1. 获取数组中间的值与tag比较，诺值>tag则想左移动，反之
     *  2. 数组为有序数组
     *  3. 找到返回索引，否则返回-1
     * */
    public static int binarySearch(int[] a, int tag) {
        int i = 0, j = a.length - 1;
        while (i < j) {
            int model = (i + j) >>> 1;
            if (tag < a[model]) { // 目标在左边
                j = model;
            } else if (tag > a[model]) { // 目标在右边
                i = model + 1;
            } else {
                return model;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] num1 = {1, 3, 7, 9, 11, 65};
        int tag = 11;
        System.out.println(binarySearch(num1, tag));
    }
}
